package com.hiray.demos.ui_architect.litho.group

import android.graphics.Color
import com.facebook.litho.annotations.FromEvent
import com.facebook.litho.annotations.OnEvent
import com.facebook.litho.annotations.Prop
import com.facebook.litho.sections.Children
import com.facebook.litho.sections.SectionContext
import com.facebook.litho.sections.annotations.GroupSectionSpec
import com.facebook.litho.sections.annotations.OnCreateChildren
import com.facebook.litho.sections.common.DataDiffSection
import com.facebook.litho.sections.common.RenderEvent
import com.facebook.litho.sections.common.SingleComponentSection
import com.facebook.litho.widget.ComponentRenderInfo
import com.facebook.litho.widget.RenderInfo
import com.facebook.litho.widget.Text
import com.facebook.yoga.YogaEdge


@GroupSectionSpec
internal object FooSectionSpec {

    @OnCreateChildren
    fun onCreateChildren(
            c: SectionContext,
            @Prop headerTitle: String,
            @Prop data: List<String>): Children {
        return Children.create()
                .child(
                        SingleComponentSection.create(c)
                                .component(
                                        Text.create(c)
                                                .text(headerTitle)
                                                .backgroundColor(Color.parseColor("#FEF4EB"))
                                                .marginDip(YogaEdge.BOTTOM, 10f)
                                                .paddingDip(YogaEdge.ALL, 15f)
                                                .build()))
                .child(
                        DataDiffSection.create<String>(c)
                                .data(data)
                                .renderEventHandler(FooSection.onRender(c)))
                .build()
    }

    @OnEvent(RenderEvent::class)
    fun onRender(
            c: SectionContext,
            @FromEvent model: String): RenderInfo {
        return ComponentRenderInfo.create()
                .component(
                        Text.create(c)
                                .text(model)
                                .marginDip(YogaEdge.TOP, 11f)
                                .paddingDip(YogaEdge.ALL, 8f)
                                .build())
                .build()
    }
}